$(function () {
    $("#oneCategoryId").change(function () {
        $("#categoryBody").empty().append("<td colspan='4' style='text-align: center;'><h5>未找到相关数据！</h5></td>");
        $("#twoCategoryId").empty().append("<option value=''> - 请选择 - </option>");
        var pId = $(this).val();
        var url = "category/getCategoryByPid.action";
        if (pId) {
            $.getJSON(url, {"pId": pId}, function (data) {
                if (data) {
                    $.each(data, function (i, field) {
                        $("#twoCategoryId").append("<option value='" + field.categoryId + "'>" + field.categoryName + "</option>");
                    });
                }
            });
        }
    });

    $("#twoCategoryId").change(function () {
        $("#categoryBody").empty().append("<td colspan='4' style='text-align: center;'><h5>未找到相关数据！</h5></td>");
    });

    $("#bind_btn").click(function () {
        var roleCategoryMap={};
        roleCategoryMap.roleId=$("#roleId").val();
        roleCategoryMap.oneCategoryId=$("#oneCategoryId").val();
        roleCategoryMap.oneCategoryName=$("#oneCategoryId>option:selected").text();
        roleCategoryMap.twoCategoryId=$("#twoCategoryId").val();
        roleCategoryMap.twoCategoryName=$("#twoCategoryId>option:selected").text();
        if(roleCategoryMap.roleId.length==0){
            smoke.alert("当前角色为空！");
            return false;
        }
        if(roleCategoryMap.oneCategoryId.length==0){
            smoke.alert("请选择一级分类！");
            return false;
        }
        if(roleCategoryMap.twoCategoryId.length==0){
            smoke.alert("请选择二级分类！");
            return false;
        }
        var data = [];
        $(":checkbox:checked").each(function (i, s) {
            var item = {};
            item.categoryId = s.id;
            item.categoryName = s.value;
            data.push(item);
        });
        if(data.length==0){
            smoke.alert("请选择三级分类！");
            return false;
        }
        $.post("category/bingCategory.action",
            {roleCategoryMap:JSON.stringify(roleCategoryMap),threeCategorys:JSON.stringify(data)},
            function(json){
                if (json&&json.success) {
                    smoke.signal(json.msg,2000);
                }else {
                    smoke.alert(json?json.msg:"请求失败！");
                }
            },"json");
    });
});